﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Xml;
using OgilvyOne.BLL;
using OgilvyOne.Model;

/// <summary>
/// 对外接口基类
/// </summary>
public class ApiBasePage : BasePage
{
    protected string type = "0";
    protected void Page_Load(object sender, EventArgs e)
    {
        string appkey = Request.Params["appkey"];
        if (!string.IsNullOrEmpty(Request.Params["from"]))
        {
            appkey = Request.Params["from"];
        }
        if (appkey != null && appkey != "" && IsAppkey(appkey.Trim()))
        {
            if (new MemberInfoBll().GetLoginMemberInfo() == null)
            {//未登录
                doNotLogin(sender, e);
            }
            else
            {//已登录
                doLogin(sender, e);
            }

        }
        else
        {
            doNotAppkey(sender, e);
        }
        XmlAttribute typeAttribute = doc.CreateAttribute("type");
        typeAttribute.Value = type;

        XmlNode result = doc.CreateElement("result");
        result.Attributes.Append(typeAttribute);
        cofco.InsertBefore(result, cofco.FirstChild);
        Response.ContentType = "text/xml";

        Response.Write(doc.OuterXml);
        Response.End(); 
        return;
    }
    protected XmlDocument doc;
    protected XmlNode cofco;
    public ApiBasePage()
    {
        doc = new XmlDocument();
        XmlDeclaration decl = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
        doc.AppendChild(decl);
        cofco = doc.CreateElement("cofco");
        doc.AppendChild(cofco);
    }
    private const string APPKEY = "key-hsk201110,key-wmw201110,";
    /// <summary>
    /// AppKey 是否有效
    /// </summary>
    /// <param name="appkey"></param>
    /// <returns></returns>
    protected bool IsAppkey(string appkey)
    {
        if (appkey != null && APPKEY.IndexOf(appkey+",") > -1)
        {
            return true;
        }
        return false;
    }

    protected virtual void doNotAppkey(object sender, EventArgs e)
    {//appkey 无效
        type = "2";
    }

    protected virtual void doLogin(object sender, EventArgs e)
    {//已登录
        type = "1";
        XmlNode user = doc.CreateElement("user");
        cofco.AppendChild(user);
        MemberInfoModel model = new MemberInfoBll().GetLoginMemberInfo();

        XmlNode loginid = doc.CreateElement("loginId");
        loginid.AppendChild(doc.CreateCDataSection(model.Username));
        user.AppendChild(loginid);

        /* XmlNode username = doc.CreateElement("username");
         username.AppendChild(doc.CreateCDataSection(model.Username));
         user.AppendChild(username);*/

        XmlNode usernickname = doc.CreateElement("usernickname");
        usernickname.AppendChild(doc.CreateCDataSection(model.MemberNickname));
        user.AppendChild(usernickname);

        XmlNode tel = doc.CreateElement("tel");
        tel.InnerText = model.Tel;
        user.AppendChild(tel);

        XmlNode moblie = doc.CreateElement("moblie");
        moblie.InnerText = model.Moblie;
        user.AppendChild(moblie);

        XmlNode email = doc.CreateElement("email");
        email.InnerText = model.Email;
        user.AppendChild(email);

        XmlNode postcode = doc.CreateElement("postcode");
        postcode.InnerText = model.Postcode;
        user.AppendChild(postcode);

        XmlNode address = doc.CreateElement("address");
        address.InnerText = model.Address;
        user.AppendChild(address);

        

    }

    protected virtual void doNotLogin(object sender, EventArgs e)
    {//未登录
        type = "0";
    }
}
